package cn.lei.netty.herostory.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * redis 使用工具类
 *
 * @author lei
 */
public final class RedisUtil {
    /**
     * 日志对象
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtil.class);
    /**
     * redis 连接池
     */
    private static JedisPool jedisPool = null;

    /**
     * 私有化默认工具类
     */
    private RedisUtil() {
    }

    /**
     * 初始化
     */
    public  static void init() {
        try {
            jedisPool = new JedisPool("127.0.0.1", 6379);
            LOGGER.info("Redis 连接成功!");
        }catch (Exception ex){
            LOGGER.error(ex.getMessage(),ex);
        }
    }

    public static Jedis getJedis(){
        if (null == jedisPool) {
            throw new RuntimeException("_jedisPool 尚未初始化");
        }

        Jedis jedis = jedisPool.getResource();
        //jedis.auth("root");

        return jedis;
    }
}
